package function

import (
	"errors"
	"fmt"
)

func SayHello() string {
	return "Hello, World!"
}

func Divide(a, b int) (int, error) {
	if b == 0 {
		return 0, errors.New("division by zero")
	}
	return a / b, nil
}

// 命名返回值函数
func DivideNamed(a, b int) (result int, err error) {
	if b == 0 {
		err = errors.New("division by zero")
		return
	}
	result = a / b
	return
}

func Sum(number ...int) int {
	total := 0
	for index, num := range number {
		total += num
		fmt.Println(index, num)
	}
	return total
}

func ApplyFunction(fn func(...int) int, nums ...int) int {
	return fn(nums...)
}

// 匿名函数和闭包
func ClosureExample() {
	x := 5
	fn := func() {
		fmt.Println(x)
	}
	fn() // Output: 5
}

func DeferExmple() {
	defer fmt.Println("world")
	fmt.Println("hello")
}
